import React, {useEffect, useRef, useState} from "react";
import {SysCompanyInfoData} from "@/pages/company/type";
import {CompanyInfoProps} from "@/pages/company/Stateless";
import {ProFormInstance} from "@ant-design/pro-form";
import {Button, message} from "antd";
import {field} from "@/pages/company/Stateless/config/filed";
import {

  sysCompanyInfoDetail,
  sysCompanyInfoUpdate
} from "@/services/system/company/api";

export default (Stateless: any) => {
  const [companyInfo, setCompany] = useState<SysCompanyInfoData>({});
  const formRef: React.MutableRefObject<ProFormInstance<SysCompanyInfoData> | undefined> = useRef<ProFormInstance<SysCompanyInfoData>>();
  useEffect(() => {
    sysCompanyInfoDetail(1).then(res=>{
      formRef.current?.setFieldsValue(res.data as SysCompanyInfoData)
      setCompany(res.data as SysCompanyInfoData)
    })
  }, [null])
  const submit: React.MouseEventHandler<HTMLElement> = (event) => {
    let data = formRef.current?.getFieldsValue()
    // @ts-ignore
    data.longitude = companyInfo.longitude;
    data.latitude = companyInfo.latitude;
    data.companyLocation = companyInfo.companyLocation
    data.id = 1
    sysCompanyInfoUpdate(data).then(res=>{
      message.success("编辑成功成功").then(res => {
      })
    })
  }
  const statelessProps: CompanyInfoProps = {
    companyInfo,
    formProps: {
      formRef,
      groupList: field(companyInfo, setCompany),
      layout: "horizontal",
      submitter: {
        resetButtonProps: {
          style: {
            display: "none"
          }
        },
        submitButtonProps: {
          style: {
            display: "none"
          }
        },
        render: () => [
          <Button onClick={(event) => submit(event)}>保存</Button>,
          <Button onClick={(event) => submit(event)}>保存</Button>,

        ]
      }
    }

  }
  return <Stateless {...statelessProps} />
}
